package com.vito.oa.act.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.base.authoriza.dao.CreateIdMapper;
import com.vito.base.util.Contants;
import com.vito.base.util.Validata;
import com.vito.oa.act.constant.OaActContants;
import com.vito.oa.act.dao.OaActProcessGroupMapper;
import com.vito.oa.act.model.ProcessGroupModel;
import com.vito.oa.act.service.OaActProcessGroupService;

/**
 * 
 * @author xzn
 *
 */
@Service
public class OaActProcessGroupServiceImpl implements OaActProcessGroupService {
	@Autowired
	private OaActProcessGroupMapper actGroupMapper;
	@Autowired
	private CreateIdMapper createIdMapper;

	@Override
	public List<ProcessGroupModel> queryGroups(ProcessGroupModel group) {
		return actGroupMapper.queryGroups(group);
	}

	@Override
	public void saveDefinedGroup(ProcessGroupModel group) {
		String id = createIdMapper.getId("act_ex_defined_group");
		if(group.getOrderNum()==null){
			group.setOrderNum(getGroupOrder(group.getCompanyId()));
		}
		group.setGroupId(id);
		group.setStatus(Contants.BASE_STATE_DIS);
		if(actGroupMapper.saveDefinedGroup(group)>0){
			addProcessToGroup(group);
		}
	}

	@Override
	public void updateGroupBase(ProcessGroupModel group) {
		actGroupMapper.updateDefinedGroup(group);
	}

	@Override
	public void updateGroupAndProcess(ProcessGroupModel group) {
		actGroupMapper.updateDefinedGroup(group);
		//先删除组下所有流程，再重新添加
		actGroupMapper.deleteAllProcessOfGroup(group.getGroupId());
		addProcessToGroup(group);
		//将没有组的流程移动到系统默认组
		addProcessToSysGroup(group.getCompanyId());
	}

	@Override
	public int deleteDefinedGroup(String groupId) {
		int count = actGroupMapper.queryGroupProcessCount(groupId);
		if(count <= 0){
			actGroupMapper.deleteDefinedGroup(groupId);
			return 1;
		}else{
			return -1;
		}
	}

	@Override
	public void addProcessToGroup(ProcessGroupModel process) {
		String[] checks = process.getCheckeds();
		if(checks != null && checks.length>0){
			//先删除与原组关系
			deleteProcessFromGroup(process);
			actGroupMapper.addProcessToGroup(process);
		}
		updateStatusAuto(process.getGroupId());
	}

	@Override
	public void deleteProcessFromGroup(ProcessGroupModel process) {
		String[] checks = process.getCheckeds();
		ProcessGroupModel model;
		String groupId;
		if(checks != null && checks.length > 0){
			for(String key : checks){
				model = actGroupMapper.queryGroupByKey(key, process.getCompanyId());
				if(model != null){
					groupId = model.getGroupId();
					if(Validata.notNullOrEmpty(groupId)){
						actGroupMapper.deleteProcessFromGroup(groupId,key);
						updateStatusAuto(model.getGroupId());
					}
				}
			}
		}
	}

	@Override
	public List<ProcessGroupModel> queryGroupsAndProcess(Map<String, String> param) {
		return actGroupMapper.queryGroupsAndProcess(param);
	}

	@Override
	public ProcessGroupModel queryGroupById(String groupId) {
		return actGroupMapper.queryGroupById(groupId);
	}

	@Override
	public int queryGroupProcessCount(String groupId) {
		return actGroupMapper.queryGroupProcessCount(groupId);
	}
	@Override
	public void addProcessToSysGroup(String companyId){
		ProcessGroupModel sysGroup = new ProcessGroupModel();
		sysGroup.setCompanyId(companyId);
		sysGroup.setDefined(Contants.PROCESS_IS_DEFINED1);
		List<ProcessGroupModel> list = actGroupMapper.queryGroups(sysGroup);
		if(list != null && !list.isEmpty()){
			sysGroup = list.get(0);
		}else{
			sysGroup.setCreateUser(Contants.USER_TYPE_SYS);
			sysGroup.setGroupName(OaActContants.PROCESS_GROUP_SYS_NAME);
			sysGroup.setOrderNum(0);
			saveDefinedGroup(sysGroup);
		}
		String[] keys =  actGroupMapper.queryProcessOutGroup(companyId);
		if(keys != null && keys.length >0){
			sysGroup.setCheckeds(keys);
			actGroupMapper.addProcessToGroup(sysGroup);
		}
	}
	private int getGroupOrder(String companyId){
		Integer order = actGroupMapper.getGroupOrder(companyId);
		order = order == null ? 1 : order+1;
		return order;
	}	
	//查询并修改组状态值
	private void updateStatusAuto(String groupId){
		int count = actGroupMapper.queryGroupProcessCount(groupId);
		String status = count > 0 ? Contants.BASE_STATE : Contants.BASE_STATE_DIS;
		ProcessGroupModel group = new ProcessGroupModel();
		group.setGroupId(groupId);
		group.setStatus(status);
		actGroupMapper.updateDefinedGroup(group);
	}

}
